package cn.wxq.daily.empdailymgforserve.controller;

import cn.wxq.daily.empdailymgforserve.domain.User;
import cn.wxq.daily.empdailymgforserve.model.Result;
import cn.wxq.daily.empdailymgforserve.model.StatusCode;
import cn.wxq.daily.empdailymgforserve.domain.SysUser;
import cn.wxq.daily.empdailymgforserve.service.IsysUserService;
import cn.wxq.daily.empdailymgforserve.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
系统用户控制器
 */
@CrossOrigin //跨越处理
@RestController
@RequestMapping("/sysUser")
@Api(value = "SysUserController|系统用户控制器")
public class SysUserController {

    @Autowired
    private IsysUserService sysUserService;

    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 测试
     *
     * @return
     */
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    @ResponseBody
    public Result test() {

        return new Result(true, 200, "成功");
    }

    /**
     * 系统用户登录
     *
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "系统用户用户登录", notes = "系统用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "username", value = "用户名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "password", value = "密码", required = true, dataType = "String"),
    })
    public Result login(String username, String password) {
        //调用登录方法，使用用户名和密码校验即可
        SysUser sysuser = sysUserService.findByUsernameAndPassword(username, password);
        if (sysuser != null) {
            /*  生成token */
            String token = jwtUtil.createJWT(sysuser.getUserId()+"", sysuser.getUsername(), "admin");
            Map map = new HashMap();
            map.put("token", token);
            map.put("name", sysuser.getUsername());
            map.put("sysUserInfo", sysuser);
            return new Result(true,StatusCode.OK,"登陆成功",map);
        } else {
            return new Result(false, StatusCode.ERROR, "登录失败，请检查用户名和密码是否正确");
        }
    }


    /**
     * 查询用户信息
     *
     * @return
     */
    @RequestMapping(value = "/findAllSysUser", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "获取全部")
    public Result findAllUser() {
        List<SysUser> sysusers = sysUserService.findAllSysUser();
        return new Result(true, StatusCode.OK, "查询成功", sysusers);
    }

    /**
     * 条件查询
     *
     * @return
     */
    @RequestMapping(value = "/findByPage", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "条件查询")
    public Result findByPage(SysUser sysUser) {
        List<SysUser> sysusers = sysUserService.findByPage(sysUser);
        if (sysusers != null){

            return new Result(true, StatusCode.OK, "查询成功", sysusers);
        }else {
            return new Result(false, StatusCode.ERROR, "数据异常");

        }
    }

    /**
     * 新增
     *
     * @return
     */
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增", notes = "新增")
    public Result insertUser(SysUser sysUser) {
        int rows = sysUserService.insertUser(sysUser);
        if (rows >= 0) {
            return new Result(true, StatusCode.OK, "新增成功");
        } else {
            return new Result(true, StatusCode.ERROR, "新增失败");
        }

    }
    /**
     * 修改
     *
     * @return
     */
    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改", notes = "修改")
    public Result updateUser(SysUser sysUser) {
        int rows = sysUserService.updateUser(sysUser);
        if (rows >= 0) {
            return new Result(true, StatusCode.OK, "修改成功");
        } else {
            return new Result(true, StatusCode.ERROR, "修改失败");
        }

    }

    /**
     * 获取用户信息
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/getUserInfoById", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "id查询", notes = "id查询")
    public Result getUserInfo(Integer userId) {
        SysUser user = sysUserService.selectUserById(userId);
        if (user != null) {
            return new Result(true, StatusCode.OK, "获取用户信息成功", user);
        } else {
            return new Result(true, StatusCode.ERROR, "获取用户信息失败");
        }

    }

    /**
     * 删除
     *
     * @return
     */
    @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除", notes = "删除")
    public Result deleteById(Integer userId) {
        int rows = sysUserService.deleteById(userId);
        if (rows >= 0) {
            return new Result(true, StatusCode.OK, "删除成功");
        } else {
            return new Result(true, StatusCode.ERROR, "删除失败");
        }

    }

}
